package com.meixiang.adapter.shopping;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import com.meixiang.R;
import com.meixiang.recyclerview.OnItemClick;
import com.meixiang.tool.Tool;
import com.meixiang.view.LineView;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 美分期分期付款列表
 *
 * @author LZRUI
 */
public class SelectStagePayAdapter extends RecyclerView.Adapter<SelectStagePayAdapter.PayViewHolder> {

    private Map<Integer, Boolean> checkStatus;
    private int lastPosition = 0;

    public SelectStagePayAdapter() {
        this.checkStatus = getStatus();
    }

    private OnItemClick itemClickListener;

    public void setOnItemClick(OnItemClick onItemClick) {
        this.itemClickListener = onItemClick;
    }

    @Override
    public PayViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_select_stage, parent, false);
        return new PayViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(final PayViewHolder holder, int position) {
        if (checkStatus.get(position) != null) {
            holder.cbCheck.setChecked(checkStatus.get(position));
        } else {
            holder.cbCheck.setChecked(false);
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Tool.isFastDoubleClick()) return;
                updateStatus(holder.getLayoutPosition());
                if (itemClickListener != null) {
                    itemClickListener.onItemClick(holder.itemView, holder.getLayoutPosition());
                }
            }
        });

        if (position != 4){
            holder.line.setMarginLeft(15);
        }
    }

    @Override
    public int getItemCount() {
        return 5;
    }

    /**
     * 初始化checkbox的状态
     *
     * @return map
     */
    private Map<Integer, Boolean> getStatus() {
        Map<Integer, Boolean> map = new HashMap<>();
        map.put(0, true);
        return map;
    }

    /**
     * 更新checkbox的状态
     *
     * @param position 位置
     */
    private void updateStatus(int position) {
        if (position != lastPosition) {
            this.checkStatus.put(position, true);
            this.checkStatus.put(lastPosition, false);
            notifyItemChanged(position);
            notifyItemChanged(lastPosition);
            lastPosition = position;
        }
    }

    static class PayViewHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.tv_stage_type)
        TextView tvStageType;
        @Bind(R.id.tv_handling_charge)
        TextView tvHandlingCharge;
        @Bind(R.id.cb_check)
        CheckBox cbCheck;
        @Bind(R.id.line)
        LineView line;

        public PayViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
